package com.yx.changdao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yx.changdao.common.entity.SysPermission;
import com.yx.changdao.common.mapper.SysPermissionMapper;
import com.yx.changdao.service.SysPermissionService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Title: SysPermissionServiceImpl
 * @Description: 系统权限服务实现
 * @Auther: Zhangjiashun
 * @Version: 1.0
 * @create 2019/11/6 11:57
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {

    /**
     * 根据权限id获取权限
     *
     * @param permissionId 权限id
     * @return 权限
     */
    @Override
    public SysPermission getPermission(String permissionId) {
        return this.getById(permissionId);
    }

    /**
     * 添加权限
     *
     * @param permissionId   权限id
     * @param permissionName 权限名
     * @return 添加后的权限
     */
    @Override
    public SysPermission addPermission(String permissionId, String permissionName) {
        SysPermission sysPermission = new SysPermission();
        sysPermission.setPermId(permissionId);
        sysPermission.setPermName(permissionName);

        return this.save(sysPermission) ? sysPermission : null;
    }

    /**
     * 刪除权限
     *
     * @param permissionId 权限id
     * @return 是否删除成功
     */
    @Override
    public Boolean delPermission(String permissionId) {
        return this.removeById(permissionId);
    }

    /**
     * 刪除权限
     *
     * @param permission 权限
     * @return 是否删除成功
     */
    @Override
    public Boolean delPermission(SysPermission permission) {
        return this.delPermission(permission.getPermId());
    }

    /**
     * 获取所有权限
     *
     * @return 权限列表
     */
    @Override
    public List<SysPermission> getAllPermissions() {
        return this.list();
    }

    /**
     * 按权限描述前缀分组获取权限列表
     *
     * @param wrapper 条件
     * @return 权限描述和权限。
     */
    @Override
    public Map<String, List<SysPermission>> permissionToMap(QueryWrapper<SysPermission> wrapper) {
        List<SysPermission> permissionList = this.list(wrapper);
        return permissionList.stream()
                .collect(Collectors.groupingBy(SysPermission::permissionDescribe));
    }
}
